// 引入mysql框架
const mysql = require('mysql')
// 连接服务器
const connection = mysql.createConnection({
    host: '127.0.0.1',
    port: "3306",
    user: "root",
    password: 'root',
    database: "lf"
})
// 转到指定首页
module.exports.index = (req, res) => {
    res.render('index.html')
}
// 添加用户
module.exports.adduser = (req, res) => {
    connection.query('insert into user(username,password) values("' + req.body.username + '","' + req.body.password + '")', (error, result) => {
        if (error) {
            console.log(error)
        } else {
            res.redirect('/show')
        }
    })
};
// 将用户的数据显示到表格中
module.exports.show = (req, res) => {
    connection.query('select * from user', (error, result) => {
        if (error) {
            console.log(error)
        } else {
            res.render('show.html', {
                list: result
            })
        }
    })
};
// 删除用户
module.exports.deluser = (req, res) => {
    connection.query('delete from user where id=' + req.query.id, (error, result) => {
        if (error) {
            console.log(error)
        }
    })
    //页面刷新
    res.redirect('/show')
};
// 跳转到修改页面
module.exports.gai = (req, res) => {
    var arr = []
    arr.push(req.query.id)
    res.render('gai.html', {
        list: arr
    })
}
// 更改数据并跳转到表格页面
module.exports.update = (req, res) => {
    connection.query('update user set username =("' + req.body.username + '"),password = ("' + req.body.password + '") where id =("' + req.body.id + '")', (error, result) => {
        if (error) {
            console.log(error)
        }
    })
    res.redirect('/show')
}